package util;

import bean.SignalEntity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class SignalEntityParse {
    public SignalEntity parse(InputStream inputStream) {
        System.out.println("Parse");
        SignalEntity signalEntity = new SignalEntity();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            String name = "";
            String value = "";
            int i = 0;
            Map<String, String> name_values = new HashMap<>();
            while ((line = br.readLine()) != null) {
                if (i % 4 == 1) {
                    name = line.substring(38, line.length() - 1);
                }
                if (i % 4 == 3) {
                    value = line;
                    name_values.put(name, value);
                    System.out.println(name + " : " + value);
                }
                i++;
            }
            if (!name_values.get("Data").equals("")) {
                signalEntity.setData(name_values.get("Data"));
            } else {
                signalEntity.setData("null");
            }

            if (!name_values.get("VehicleNumber").equals("")) {
                signalEntity.setVehicleNumber(Integer.parseInt(name_values.get("VehicleNumber")));
            } else {
                signalEntity.setVehicleNumber(-1);
            }

            if (!name_values.get("TrainNumber").equals("")) {
                signalEntity.setTrainNumber(name_values.get("TrainNumber"));
            } else {
                signalEntity.setTrainNumber("null");
            }

            if (!name_values.get("Bogie").equals("")) {
                signalEntity.setBogie(Integer.parseInt(name_values.get("Bogie")));
            } else {
                signalEntity.setBogie(-1);
            }

            if (!name_values.get("Axle").equals("")) {
                signalEntity.setAxle(Integer.parseInt(name_values.get("Axle")));
            } else {
                signalEntity.setAxle(-1);
            }
            if(name_values.get("PathOfTem")!=null) {
                if (!name_values.get("PathOfTem").equals("")) {
                    signalEntity.setPathOfTem(name_values.get("PathOfTem"));
                } else {
                    signalEntity.setPathOfTem("null");
                }
            }
            if(name_values.get("PathOfVibrate")!=null) {
                if (!name_values.get("PathOfVibrate").equals("")) {
                    signalEntity.setPathOfVibrate(name_values.get("PathOfVibrate"));
                } else {
                    signalEntity.setPathOfVibrate("null");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return signalEntity;
    }
}
